package com.smart.wap.app;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.lang.Opt;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.core.BaseSearch;
import com.smart.common.entities.core.Page;
import com.smart.manager.wiki.entities.enums.NoticeScope;
import com.smart.manager.wiki.entities.models.WikiDocAgreement;
import com.smart.manager.wiki.entities.models.WikiDocStrategy;
import com.smart.manager.wiki.entities.models.WikiIntelNotice;
import com.smart.manager.wiki.entities.models.WikiIntelRemind;
import com.smart.manager.wiki.entities.models.query.QWikiDocStrategy;
import com.smart.manager.wiki.entities.repository.DocAgreementRepository;
import com.smart.manager.wiki.entities.repository.IntelNoticeRepository;
import com.smart.manager.wiki.entities.repository.IntelRemindRepository;
import com.smart.utils.MemberAcl;
import com.smart.utils.MemberStp;
import com.smart.wap.bodys.WinkBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author Administrator
 */

@Api(tags = "移动端-协议通知提醒")
@RestController
@RequestMapping(SmartConstant.WAP_API_PREFIX + "wiki")
@RequiredArgsConstructor
public class WikiController {

    private final DocAgreementRepository docAgreementRepository;
    private final IntelNoticeRepository noticeRepository;
    private final IntelRemindRepository remindRepository;


    @GetMapping(value = "agreement/{code}")
    @ApiOperation(value = "JSON版协议")
    public AjaxResult<WikiDocAgreement> readAgreement(@PathVariable String code) {
        WikiDocAgreement result = docAgreementRepository.readOne(code, true, true);
        return AjaxResult.data(result);
    }


    @GetMapping(value = "strategy")
    @ApiOperation(value = "WTB-玩法介绍列表")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<Page<WikiDocStrategy>> strategy(@Validated WinkBody.StrategySearch search) {
        QWikiDocStrategy query = new QWikiDocStrategy();
        Opt.ofBlankAble(search.getKind()).ifPresent(x -> query.kind.eq(x));
        query.order().sort.asc().order().id.desc();
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows());
        Page<WikiDocStrategy> result = new Page<WikiDocStrategy>().load(query.findPagedList());
        return AjaxResult.data(result);

    }


    @GetMapping(value = "notice/list")
    @ApiOperation(value = "系统通知列表")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<Page<WikiIntelNotice>> noticeList(@Validated BaseSearch search) {
        Page<WikiIntelNotice> result = noticeRepository.getPageList(NoticeScope.MEMBER, MemberAcl.getLoginId(), search);
        return AjaxResult.data(result);
    }


    @GetMapping(value = "notice/detail/{id}")
    @ApiOperation(value = "系统通知详情")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<WikiIntelNotice> noticeDetail(@PathVariable Long id) {
        WikiIntelNotice detail = noticeRepository.getDetail(MemberAcl.getLoginId(), id);
        return AjaxResult.data(detail);
    }


    @GetMapping(value = "remind/list")
    @ApiOperation(value = "消息提醒列表")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<Page<WikiIntelRemind>> remindList(@Validated WinkBody.RemindSearchBody search) {
        Page<WikiIntelRemind> result = remindRepository.getPageList(NoticeScope.MEMBER, MemberAcl.getLoginId(), search);
        return AjaxResult.data(result);
    }


    @GetMapping(value = "remind/read/{remindId}")
    @ApiOperation(value = "提醒单条已读", notes = "路径参数为消息ID")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<String> remindList(@PathVariable Long remindId) {
        int i = remindRepository.changeToRead(NoticeScope.MEMBER, MemberAcl.getLoginId(), remindId);
        return AjaxResult.success(String.format("成功设置%s条提醒消息为已读", i));
    }


    @GetMapping(value = "remind/oneKeyRead")
    @ApiOperation(value = "提醒一键已读")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<String> oneKeyRead() {
        int i = remindRepository.oneKeyToRead(NoticeScope.MEMBER, MemberAcl.getLoginId());
        return AjaxResult.success(String.format("成功设置%s条提醒消息为已读", i));
    }


}
